package main

import (
	pb "ihos/ihos"
)

type VideoPadSettings struct {
	id              int32 //PK
	hzid            int32
	name            string
	sex             string
	age             string
	mobile          string
	address         string
	sn              string
	room            string
	playspeed       string
	idletimeout     string
	clockeffect     string
	videocalllayout string
}

func (d *VideoPadSettings) GetFromDB(id int32) error {
	sql := "SELECT id,hzid,name,sex,age,mobile,address,sn,room,playspeed,idletimeout,clockeffect,videocalllayout FROM videopad_settings WHERE id=$1"
	return DBConn.db.QueryRow(sql, id).Scan(&d.id, &d.hzid, &d.name, &d.sex, &d.age, &d.mobile, &d.address, &d.sn, &d.room, &d.playspeed, &d.idletimeout, &d.clockeffect, &d.videocalllayout)
}

func (d *VideoPadSettings) GetFromDBByHZID(hzid int32) error {
	sql := "SELECT id,hzid,name,sex,age,mobile,address,sn,room,playspeed,idletimeout,clockeffect,videocalllayout FROM videopad_settings WHERE hzid=$1"
	return DBConn.db.QueryRow(sql, hzid).Scan(&d.id, &d.hzid, &d.name, &d.sex, &d.age, &d.mobile, &d.address, &d.sn, &d.room, &d.playspeed, &d.idletimeout, &d.clockeffect, &d.videocalllayout)
}

func (d *VideoPadSettings) SaveToDB(id int32) error {
	sql := ""
	var err error
	if id > 0 { //update
		sql = "UPDATE videopad_settings set name=$1,sex=$2,age=$3,mobile=$4,address=$5,sn=$6,room=$7,playspeed=$8,idletimeout=$9,clockeffect=$10,videocalllayout=$11 where hzid=$12 RETURNING id"
		err = DBConn.db.QueryRow(sql, d.name, d.sex, d.age, d.mobile, d.address, d.sn, d.room, d.playspeed, d.idletimeout, d.clockeffect, d.videocalllayout, d.hzid).Scan(&d.id)
	} else { //insert
		sql = "INSERT INTO videopad_settings(hzid,name,sex,age,mobile,address,sn,room,playspeed,idletimeout,clockeffect,videocalllayout)VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) RETURNING id"
		err = DBConn.db.QueryRow(sql, d.hzid, d.name, d.sex, d.age, d.mobile, d.address, d.sn, d.room, d.playspeed, d.idletimeout, d.clockeffect, d.videocalllayout).Scan(&d.id)
	}
	return err
}

func (w *VideoPadSettings) toPB() *pb.VideoPadSettings {
	p := &pb.VideoPadSettings{
		HZID:            w.hzid,
		Name:            w.name,
		Sex:             w.sex,
		Age:             w.age,
		Mobile:          w.mobile,
		Address:         w.address,
		SN:              w.sn,
		Room:            w.room,
		PlaySpeed:       w.playspeed,
		IdleTimeout:     w.idletimeout,
		ClockEffect:     w.clockeffect,
		VideoCalllayout: w.videocalllayout,
	}
	return p
}

func (w *VideoPadSettings) fromPB(p *pb.VideoPadSettings) *VideoPadSettings {
	w.hzid = p.GetHZID()
	w.name = p.GetName()
	w.sex = p.GetSex()
	w.age = p.GetAge()
	w.mobile = p.GetMobile()
	w.address = p.GetAddress()
	w.sn = p.GetSN()
	w.room = p.GetRoom()
	w.playspeed = p.GetPlaySpeed()
	w.idletimeout = p.GetIdleTimeout()
	w.clockeffect = p.GetClockEffect()
	w.videocalllayout = p.GetVideoCalllayout()
	return w
}
